/*
* 轮播图
* */
import AppConfig from '../config/appConfig';
import axiosUtil from "../utils/axiosUtil";
import AppStore from "../store/appStore";
import {TimeStamp} from "../utils/utils";
import md5 from 'md5';
import {Sign} from "../utils/TBUtil";

export function GetSwiper(item) {
  if (item.TypeFrom === AppConfig.JinDong){
    return JinDong(item)
  }
  if (item.TypeFrom === AppConfig.TaoBao){
    return TaoBao(item);
  }
  if (item.TypeFrom === AppConfig.PingDuoDuo){
    return PingDuoDuo(item);
  }
  return new Promise(resolve => {
     resolve(null);
  })
}

//获取京东
 function JinDong(item) {
  return new Promise(resolve => {
    //获取京东商品数据

    var  url =  AppStore.data.jinDongApi.Swiper.format( TimeStamp(),item.Id)
    axiosUtil.get(url).then(res=>{
      if (res.code !== '0' || res.data===null || res.data.itemAnnexVos === null || res.data.itemAnnexVos.length <=0){
        resolve(item)
      }else {

        item.Cid = res.data.cid
        item.goods_gallery_urls=[]

        res.data.itemAnnexVos.map((picItem,index)=>{
          // console.log(picItem)
          item.goods_gallery_urls.push(picItem.annexUrl)
          resolve(item)
        })
      }
    })
  })
}
//淘宝轮播图
 function TaoBao(item) {
  return new Promise(resolve => {
    console.log('轮播图')
    const data = Sign(AppStore.data.taoBaoApi.app_key,AppStore.data.taoBaoApi.app_secret,
        "taobao.tbk.item.info.get",{
        num_iids:item.Id
      }
      )

    // const timestamp = TimeStamp();
    // //
    // const signStr = AppStore.data.taoBaoApi.app_secret+
    //   "app_key"+AppStore.data.taoBaoApi.app_key+
    //   "format"+'json'+
    //   "method"+"taobao.tbk.item.info.get"+
    //   "num_iids"+ item.Id+
    //   "sign_method"+"md5"+
    //   "timestamp"+timestamp+
    //   "v"+"2.0"+
    //   AppStore.data.taoBaoApi.app_secret;
    // console.log("====>")
    // console.log(signStr)
    // const signback = md5(signStr);

    var  url =  AppStore.data.taoBaoApi.api
    // var  data ={}
    //
    // data.app_key = AppStore.data.taoBaoApi.app_key
    // data.format = 'json'
    // data.method = "taobao.tbk.item.info.get"
    // data.num_iids = item.Id
    // data.sign_method = 'md5'
    // data.timestamp = timestamp
    // data.v = '2.0'
    // data.sign =signback.toUpperCase()

    axiosUtil.get(url,data).then(res=>{
       if (res.tbk_item_info_get_response===null){
         resolve(item)
       } else {
         console.log(res)
         var detalItem = res.tbk_item_info_get_response.results.n_tbk_item[0]

          item.goods_gallery_urls=[]
         detalItem.small_images.string.map((picItem,index)=>{
             item.goods_gallery_urls.push(picItem)
             item.item_url  = detalItem.item_url

             //获取淘口令
             // if(item.Tpwd === ''){
             //
             // }else {
             //   resolve(item)
             // }
           resolve(item)
          })
       }
    })
  })
}
//拼多多轮播图
 function PingDuoDuo(item) {
  return new Promise(resolve => {
    //获取京东商品数据
    const timestamp = TimeStamp();
    // console.log(item.Id)
    const sign =
      AppStore.data.pingDuoDuoApi.client_secret +
      "client_id"+AppStore.data.pingDuoDuoApi.client_id+
      "data_type"+"JSON"+
      "goods_id_list"+"["+item.Id+"]"+
      "timestamp" + timestamp+
      "type"+AppStore.data.pingDuoDuoApi.Swiper+
      AppStore.data.pingDuoDuoApi.client_secret;

    const signback = md5(sign);

    var  data = {}
    data.client_id = AppStore.data.pingDuoDuoApi.client_id
    data.data_type = "JSON"
    data.goods_id_list ="["+item.Id+"]"  //奇葩成功
    data.timestamp = timestamp;
    data.type = AppStore.data.pingDuoDuoApi.Swiper
    data.sign =signback.toUpperCase()

    var  url = AppStore.data.pingDuoDuoApi.api

    axiosUtil.post(url,data).then(res=>{

       if (res.goods_detail_response ===null){
         resolve(item)
       } else {
          var detalItem =res.goods_detail_response.goods_details[0]

         item.goods_gallery_urls=[]
         detalItem.goods_gallery_urls.map((picItem,index)=>{
           item.goods_gallery_urls.push(picItem)
           resolve(item)
         })
       }
    })
  })
}
